<?php

namespace App\Http\Middleware;

use App\services\JwtService;
use Closure;
use Illuminate\Http\Request;

class CheckJwt
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        //获取token   //转换token 获取id值   封装公共层 方便多次提取
        $token = $request->header('token');

        if (!$token){

            exit("没有token 或token  错误");
        }
        $uid= (new JwtService())->decode($token)['id'];
        if (!isset($uid)||empty($uid)){

            exit("token  错误，没有id");
        }

        $request->user_id=$uid;

        return $next($request);
    }
}
